using Fusion.Hotfix.Mono;
using System;
using UnityEngine;

namespace Fusion.Hotfix
{
    public static class HotfixExtensions
    {
        public static DLLMonoBase AddMono<T>(this GameObject gameObject, string data) where T : DLLMonoBase
        {
            return AddMono(gameObject, typeof(T), data);
        }

        public static DLLMonoBase AddMono(this GameObject gameObject, Type type, string data)
        {
            return AddMono(gameObject, type.ToString(), data);
        }

        public static DLLMonoBase AddMono(this GameObject gameObject, string type, string data)
        {
            DLLMonoBehaviour monoBehaviour = gameObject.AddComponent<DLLMonoBehaviour>();
            monoBehaviour.MonoDataAsset = data;
            monoBehaviour.ClassFullName = type;
            return monoBehaviour.MonoBase;
        }

        public static T GetMono<T>(this GameObject gameObject) where T : DLLMonoBase
        {
            DLLMonoBase mono = GetMono(gameObject, typeof(T));
            return mono != null ? (T)mono : null;
        }

        public static DLLMonoBase GetMono(this GameObject gameObject, Type type)
        {
            return GetMono(gameObject, type.ToString());
        }

        public static DLLMonoBase GetMono(this GameObject gameObject, string type)
        {
            DLLMonoRecorder recorder = gameObject.GetComponent<DLLMonoRecorder>();
            if (recorder != null)
            {
                return recorder.GetMono(type);
            }
            else
            {
                return null;
            }
        }

        public static T[] GetMonos<T>(this GameObject gameObject) where T : DLLMonoBase
        {
            DLLMonoBase[] monos = GetMonos(gameObject, typeof(T));
            return monos != null ? (T[])monos : null;
        }

        public static DLLMonoBase[] GetMonos(this GameObject gameObject, Type type)
        {
            return GetMonos(gameObject, type.ToString());
        }

        public static DLLMonoBase[] GetMonos(this GameObject gameObject, string type)
        {
            DLLMonoRecorder recorder = gameObject.GetComponent<DLLMonoRecorder>();
            if (recorder != null)
            {
                return recorder.GetMonos(type);
            }
            else
            {
                return null;
            }
        }
    }
}

